#coding=utf-8
import os, sys, datetime, time
import tsina
os.environ['DJANGO_SETTINGS_MODULE'] = 'gfw.settings'
from tsina.dbserver.models import Message, User
from django.db import IntegrityError

from optparse import OptionParser
parser = OptionParser()

parser.add_option('-k', '--keyword', dest='keyword',help=u'关键词')
(options, args) = parser.parse_args()      
    
n = 1000
search_url = 'http://t.sina.com.cn/k/%s&page=%i'
keyword = options.keyword
print keyword

t = tsina.t

data = []

j= 0
for i in range(n):    
  url = search_url%(keyword, i+1)
  print url
  now = datetime.datetime.now()
  code, r,c = t._get_url(url)
  ms = tsina.parse_search(c, now)
    
  for m in ms:
    u = User.objects.get_or_create(user_id=m.owner_id)[0]
    message = Message(user=u, url_id=m.url_id, resource_id=m.resource_id, keyword=keyword, content=m.content, published_at=m.created_at)
    
    try:
      message.save()
    except IntegrityError:
      j = j + 1
      if j > 50: 
        print 'exiting'
        sys.exit()
      else:
        continue
      
    else:
      i = 0
      
  time.sleep(60)
